import { Vec3 } from "cc";
import { Vec3Util } from "db://app-game/core/utils/Vec3Util";
import { ecs } from "db://app-game/libs/ecs/ECS";
import { smc } from "../../../common/ecs/model/SingletonModuleComp";
import { RoleAttarModelComp } from "../model/RoleAttarModelComp";
import { RoleControlComp } from "../model/RoleControlComp";
import { RoleMoveModelComp } from "../model/RoleMoveModelComp";
import { Role } from "../Role";
import { RoleSkillType } from "../Role.type";
import { AnimatorSwitchManage } from "../view/animator/AnimatorSwitchManage";

/** 角色碰撞系统 */
@ecs.register("RoleControl")
export class RoleControlSystem
  extends ecs.ComblockSystem
  implements ecs.ISystemUpdate
{
  RoleView: any;
  filter(): ecs.IMatcher {
    return ecs.allOf(RoleControlComp, RoleMoveModelComp, RoleAttarModelComp);
  }
  update(role: Role, dt: number): void {
    /** 动画切换管理 */
    const aSwitchManage = role.RoleView.animatorSwitchManage;
    this.joysitickControllerRole(role, aSwitchManage);
  }

  /** 摇杆控制 */
  joysitickControllerRole(role: Role, aSwitchManage: AnimatorSwitchManage) {
    const { moveJoysitick, shootJoysitick, closeCombatJoysitick } =
      smc.level.UILayerEntityGroup;

    let angle = Vec3.ZERO;
    let velocity = Vec3.ZERO;
    let speed = role.RoleAttarModel.speed;

    // 1.查看角色方向
    if (shootJoysitick?.JoysitickControl.active) {
      // 1.判断射击是否激活
      angle = shootJoysitick?.JoysitickControl.input;
    } else if (closeCombatJoysitick?.JoysitickControl.active) {
      // 2.判断攻击是否激活
      angle = closeCombatJoysitick?.JoysitickControl.input;
    } else if (moveJoysitick.JoysitickControl.active) {
      // 3.判断移动是否激活
      angle = moveJoysitick.JoysitickControl.input;
    }

    // 2.查看角色移动速度
    if (shootJoysitick?.JoysitickControl.active) {
      if (moveJoysitick.JoysitickControl.active) {
        velocity = moveJoysitick.JoysitickControl.input
          .clone()
          .multiplyScalar(speed);
      }

      this.roleShoot(role, aSwitchManage, velocity, angle);
    } else if (
      closeCombatJoysitick?.JoysitickControl.active &&
      !closeCombatJoysitick?.JoysitickControl.coolingState
    ) {
      this.roleCloseCombat(role, aSwitchManage, velocity, angle);
    } else if (moveJoysitick.JoysitickControl.active) {
      if (moveJoysitick.JoysitickControl.stickLength > 40) {
        velocity = moveJoysitick.JoysitickControl.input
          .clone()
          .multiplyScalar(speed * 4);
        this.roleRun(role, aSwitchManage, velocity, angle);
      } else {
        velocity = moveJoysitick.JoysitickControl.input
          .clone()
          .multiplyScalar(speed);
        this.roleWalk(role, aSwitchManage, velocity, angle);
      }
    } else {
      this.roleIdle(role, aSwitchManage);
    }
  }

  /** 角色闲置 */
  roleIdle(role: Role, aSwitchManage: AnimatorSwitchManage) {
    // 1.判断切换状态是否成功
    const a = aSwitchManage.lowerIdle();
    const b = aSwitchManage.upperIdle();
    const switchSuccess = aSwitchManage.matcherVague(a, b);
    if (!switchSuccess) return;
    // 1.闲置角色移动速度归零
    role.RoleMoveModel.velocity = new Vec3();
  }

  /** 角色行走 */
  roleWalk(
    role: Role,
    aSwitchManage: AnimatorSwitchManage,
    velocity: Vec3,
    angle: Vec3
  ) {
    // 1.判断切换状态是否成功
    const a = aSwitchManage.lowerWalk();
    const b = aSwitchManage.upperWalk();
    const switchSuccess = aSwitchManage.matcherVague(a, b);
    if (!switchSuccess) return;
    role.RoleMoveModel.velocity = velocity;
    role.RoleMoveModel.angle = Vec3Util.getAngle(angle);
  }

  /** 角色奔跑 */
  roleRun(
    role: Role,
    aSwitchManage: AnimatorSwitchManage,
    velocity: Vec3,
    angle: Vec3
  ) {
    const a = aSwitchManage.lowerRun();
    const b = aSwitchManage.upperRun();
    const switchSuccess = aSwitchManage.matcherVague(a, b);
    if (!switchSuccess) return;
    role.RoleMoveModel.velocity = velocity;
    role.RoleMoveModel.angle = Vec3Util.getAngle(angle);
  }

  /** 角色近战攻击 */
  roleCloseCombat(
    role: Role,
    aSwitchManage: AnimatorSwitchManage,
    velocity: Vec3,
    angle: Vec3
  ) {
    const a = aSwitchManage.lowerRun();
    const b = aSwitchManage.upperSkill(RoleSkillType.Attack);
    const switchSuccess = aSwitchManage.matcherVague(a, b);
    if (!switchSuccess) return;
    role.RoleMoveModel.velocity = velocity;
    role.RoleMoveModel.angle = Vec3Util.getAngle(angle);
  }

  /** 角色射击 */
  roleShoot(
    role: Role,
    aSwitchManage: AnimatorSwitchManage,
    velocity: Vec3,
    angle: Vec3
  ) {
    const a = aSwitchManage.lowerRun();
    const b = aSwitchManage.upperSkill(RoleSkillType.Shoot);
    const switchSuccess = aSwitchManage.matcherVague(a, b);
    if (!switchSuccess) return;
    role.RoleMoveModel.velocity = velocity;
    role.RoleMoveModel.angle = Vec3Util.getAngle(angle);
  }
}
